package com.gljx.web.service.impl;

import com.gljx.constant.ActivitiKey;
import com.gljx.util.*;
import com.gljx.web.Entity.*;
import com.gljx.web.dao.*;
import com.gljx.web.service.ChangecoachTwoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/28 0028.
 */
@Transactional
@Service
public class ChangecoachTwoServiceImpl extends CommonDao implements ChangecoachTwoService {

    @Autowired
    ChangecoachtwoMapper changecoachtwoMapper;
    @Autowired
    ChangecoachtwoentryMapper changecoachtwoentryMapper;
    @Autowired
    ExerciseCarPlanMapper exerciseCarPlanMapper;
    @Autowired
    StudentMapper studentMapper;
    @Autowired
    CarMapper carMapper;
    @Autowired
    CoachsubMapper coachsubMapper;
    @Autowired
    HttpSession session;

    @Override
    public Map<String, Object> selectPage(int pindex, String psql, int psize) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Pindex", pindex);
        paramMap.put("Psql", psql);
        paramMap.put("Psize", psize);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
        this.getSqlSession().select("com.gljx.web.dao.ChangecoachtwoMapper.pagemap", paramMap, null);
        return paramMap;
    }

    /**
     * @Author lcx
     * @Date 2017/11/28 0028 下午 3:12
     * 查询子类
     */
    @Override
    public List<Changecoachtwoentry> selectChangecoachTwoentryByFid(String fid, String cfstudentname) {
        return changecoachtwoentryMapper.selectChangecoachTwoentryByFparentid(fid, cfstudentname);
    }

    /**
     * @Author lcx
     * @Date 2017/11/28 0028 下午 3:12
     * 添加
     */
    @Override
    public void addChangecoachTwo(Changecoachtwo changecoachtwo, String[] cfstudentid, String[] cfstudentno,
                                  String[] cfmobile, String[] cfphone, String[] cfcoachid2, String[] cfcoachid,
                                  String[] cfcoachno, String[] cfremark, String state) {
        //删除老的信息在次添加
        if ("1".equals(state)) {
            //查询之前的信息
            Changecoachtwo changecoachtwotem = changecoachtwoMapper.selectByPrimaryKey(changecoachtwo.getFid());
            changecoachtwo.setFcreatorid(changecoachtwotem.getFcreatorid());
            changecoachtwo.setFcreatetime(changecoachtwotem.getFcreatetime());
            changecoachtwoMapper.deleteByPrimaryKey(changecoachtwo.getFid());
            changecoachtwoentryMapper.deleteByFparentid(changecoachtwo.getFid());
            //为了消除提交页面点击确定丢失审核人的 问题
            if ("2".equals(changecoachtwo.getFfivouchered() + "")) {
                changecoachtwo.setFauditorid(changecoachtwotem.getFauditorid());
                changecoachtwo.setCfbillstate(Long.valueOf("3"));
            }
            //提交
            if ("4".equals(changecoachtwo.getFfivouchered() + "")) {
                Axamine axamine = new Axamine();
                axamine.submissionProcess(String.valueOf(changecoachtwo.getFnumber()));

            }

        }

        /*获取uuid*/
        String uuid = GetFid.UUIDIntoFid();
        changecoachtwo.setFid(uuid);

        for (int i = 0; i < cfstudentid.length; i++) {
            Changecoachtwoentry changecoachtwoentry = new Changecoachtwoentry();
            changecoachtwoentry.setFid(GetFid.UUIDIntoFid());
            changecoachtwoentry.setFparentid(uuid);
            changecoachtwoentry.setCfstudentid(cfstudentid[i]);
            changecoachtwoentry.setCfstudentno(cfstudentno[i]);
            changecoachtwoentry.setCfmobile(cfmobile[i]);
            changecoachtwoentry.setCfphone(cfphone[i]);
            changecoachtwoentry.setCfcurrentcoachid(cfcoachid2[i]);
            changecoachtwoentry.setCfcoachid(cfcoachid[i]);
            changecoachtwoentry.setCfcoachno(cfcoachno[i]);
            changecoachtwoentry.setCfremark(cfremark[i]);

            changecoachtwoentryMapper.insertSelective(changecoachtwoentry);
        }
        changecoachtwoMapper.insertSelective(changecoachtwo);
    }

    /**
     * @Author lcx
     * @Date 2017/11/28 0028 下午 3:12
     * 更新列表的查询
     */
    @Override
    public Changecoachtwo selectChangecoachTwoByFid(String fid) {
        Changecoachtwo changecoachtwo = changecoachtwoMapper.selectByPrimaryKey(fid);
        if (changecoachtwo != null) {
            changecoachtwo.setChangecoachtwoentryList(changecoachtwoentryMapper.selectChangecoachTwoentryByFparentid(fid, ""));
        }
        return changecoachtwo;
    }

    //删除
    @Override
    public void deleteByChangecoachTwoKey(String[] deleteId, String[] fnumber) {
        ActitiviUtil actitiviUtil = new ActitiviUtil();

        for (int i = 0; i < deleteId.length; i++) {
            actitiviUtil.deleteTask(ActivitiKey.PUBLIC_KEY, fnumber[i]);
            changecoachtwoMapper.deleteByPrimaryKey(deleteId[i]);
            changecoachtwoentryMapper.deleteByFparentid(deleteId[i]);
        }
    }

    @Override
    public ServerResponse examine(Changecoachtwo changecoachtwo, String state, String agreed, String auditcomment) {
        //如果是审核
        Axamine axamine = new Axamine();
        if ("5".equalsIgnoreCase(state)) {
            ServerResponse serverResponse = axamine.auditProcess(changecoachtwo.getFnumber(), agreed, auditcomment);
            //如果返回 1 代表 流程结束 2代表流程还在继续 0 代表错误
            if (serverResponse.getStatus() == ResponseCode.SUCCESS.getCode()) {
                changecoachtwo.setCfbillstate(Long.valueOf("5"));

                //获取操作人信息
                User user = (User) session.getAttribute("user");

                //获取当前修改时间
                String createTime = TimeFormat.timeFormat(new Date());
                //修改时间
                changecoachtwo.setFlastupdatetime(createTime);
                //修改人id
                changecoachtwo.setFauditorid(user.getFid());
                changecoachtwoMapper.updateByPrimaryKeySelective(changecoachtwo);
            }
            //如果是不同意状态则修改单据状态为保存
            if ("2".equals(agreed)) {
                changecoachtwo.setCfbillstate(Long.valueOf("3"));
                changecoachtwoMapper.updateByPrimaryKeySelective(changecoachtwo);
            }
            return serverResponse;
        }
        if ("3".equalsIgnoreCase(state)) {
            changecoachtwo.setCfbillstate(Long.valueOf("3"));
            changecoachtwo.setFsourcefunction(auditcomment);
            axamine.againstAuditProcess(changecoachtwo.getFnumber(), auditcomment);
            changecoachtwoMapper.updateByPrimaryKeySelective(changecoachtwo);
            return ServerResponse.createBySuccessMessage("反审核成功！");
        }

        return ServerResponse.createByError();
    }

    @Override
    public List<Changecoachtwoentry> selectChangecoachTworegAsXls(String fnumber, String fdescription, String cfstudentname, String beginTime, String endTime, String fcompanyid) {
        return changecoachtwoMapper.selectChangecoachtworegAsXls(fnumber, fdescription, cfstudentname, beginTime, endTime, fcompanyid);
    }

    @Override
    public Map<String, Object> selectPageCount(String psql) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("Psql", psql);
        paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
        this.getSqlSession().select("com.gljx.web.dao.ChangecoachtwoMapper.pagecount", paramMap, null);
        return paramMap;
    }

    @Override
    public String importPracticeDrivingData(String plan) {
        ExerciseCarPlan exerciseCarPlan = exerciseCarPlanMapper.selectByPrimaryKey(plan);//练车时间表
        if(exerciseCarPlan==null){
            return null;
        }
        return (String) exerciseCarPlan.getFnameL2();
    }

    /**
     * 导入到科目二分车
     */
//    public Map<String, Object> importPracticeDrivingData(List<List<Object>> list) {
//        String time = exerciseCarPlanMapper.selectExerciseCarPlanByFnumber(list.get(0).get(1));//练车时间表
//        for (int i = 2; i < list.size(); i++) {
//            Map<String, String> map = new HashMap<>();
//            map.put("fnumber", (String) list.get(i).get(0));
//            List<Car> car = carMapper.selectCarByFnumber(map);//车辆表;//车号
//            List<Coachsub> coachsubs = coachsubMapper.selectCoachSubAsXls((String) list.get(i).get(1), null, null, null, null);// 教练表;//教练员
//            List<Student> students = studentMapper.selectStudentByCfidcardno((String) list.get(i).get(2));//学员身份证查询;//学员身份证号
//            students.get(0).getFid();//学员ID
//            students.get(0).getCfidcardno();//学员身份证
//            coachsubs.get(0).getFid();
//        }
//        return null;
//    }
}
